package com.example.mcp.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * API测试工具类
 * 用于直接测试中转服务的响应格式
 * 
 * @author Generated
 * @version 1.0.0
 */
public class ApiTestUtil {
    
    private static final String API_URL = "https://metamrb.zenymes.com/v1/chat/completions";
    private static final String API_KEY = "sk-ZSbPzoJIOaIxKmaqQPjfG4KccBJpVKFENDvN2hZkl7zBLYoo";
    
    public static void testApiResponse() {
        RestTemplate restTemplate = new RestTemplate();
        ObjectMapper objectMapper = new ObjectMapper();
        
        try {
            // 构建请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setBearerAuth(API_KEY);
            
            // 构建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "gemini-2.5-pro");
            requestBody.put("messages", List.of(
                Map.of("role", "user", "content", "Hello, please say hi")
            ));
            requestBody.put("temperature", 0.7);
            requestBody.put("max_tokens", 20000);
            
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, headers);
            
            // 发送请求
            System.out.println("发送请求到: " + API_URL);
            System.out.println("请求体: " + objectMapper.writeValueAsString(requestBody));
            
            ResponseEntity<String> response = restTemplate.exchange(
                API_URL, 
                HttpMethod.POST, 
                request, 
                String.class
            );
            
            // 打印响应
            System.out.println("响应状态: " + response.getStatusCode());
            System.out.println("响应头: " + response.getHeaders());
            System.out.println("响应体: " + response.getBody());
            
        } catch (Exception e) {
            System.err.println("API测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        testApiResponse();
    }
}
